//
//  WBNavigationViewController.swift
//  YUANSHI
//
//  Created by pro on 2019/12/7.
//  Copyright © 2019 wangbin. All rights reserved.
//

import UIKit

class WBNavigationViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setNavBarAppearence()
               
        self.navigationBar.barTintColor = knavmainColor
               
        self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
               
        self.delegate = self
               
        self.interactivePopGestureRecognizer?.delaysTouchesBegan = true
        self.interactivePopGestureRecognizer?.delegate = self
        self.interactivePopGestureRecognizer?.isEnabled = true
               
        self.navigationBar.shadowImage = UIImage.init()
        self.navigationBar.setBackgroundImage(UIImage.init(), for: .default)
    }

}

func setNavBarAppearence() {
    
            // 设置导航栏默认的背景颜色
    //        WRNavigationBar.defaultNavBarBarTintColor = UIColor.init(hexColor: "26a8fe")
            // 设置导航栏所有按钮的默认颜色
    //        WRNavigationBar.defaultNavBarTintColor = UIColor.white
            // 设置导航栏标题默认颜色
    //        WRNavigationBar.defaultNavBarTitleColor = .white
            // 统一设置状态栏样式
    //        WRNavigationBar.defaultStatusBarStyle = .lightContent
            // 如果需要设置导航栏底部分割线隐藏，可以在这里统一设置
    //        WRNavigationBar.defaultShadowImageHidden = true
}

extension WBNavigationViewController{
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        
        
        if children.count > 0 {
                    viewController.hidesBottomBarWhenPushed = true
                    
                    //添加图片
        //            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "navigation_left_back")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(leftClick))
        //            //添加文字
        //            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "返回", style: .plain, target: self, action: #selector(leftClick))
                    //隐藏底部分栏
                    
                    let backBtn = UIButton(type: .custom)
                    
                    //返回按钮的图片-状态
                    backBtn.setImage(UIImage(named: "bx_back_black"), for: .normal)
                    
                    //添加事件
                    backBtn.addTarget(self, action: #selector(leftClick), for: .touchUpInside)
                    
                    backBtn.contentHorizontalAlignment = .left
                    backBtn.size = CGSize(width: 70, height: 32)
                    
                    viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backBtn)
                    
                }
                super.pushViewController(viewController, animated: animated)
    }
    
    //返回上一层控制器
    @objc func leftClick()  {
        
        popViewController(animated: true)
        
    }
}

extension WBNavigationViewController: UIGestureRecognizerDelegate {
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if self.viewControllers.count <= 1 {
            return false
        }
        return true
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

extension WBNavigationViewController: UINavigationControllerDelegate {
    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
//        var isCustomTransition = false
//        if operation == .push {
//            if let testVC = toVC as? CountdownViewController {
//                isCustomTransition = testVC.isNeedCustomTransition
//            }
//        }else if operation == .pop {
//            if let testVC = fromVC as? CountdownViewController {
//                isCustomTransition = testVC.isNeedCustomTransition
//            }
//        }
//        if isCustomTransition {
//            return JXArticleFloatRoundEntryAnimator(operation: operation, sourceCenter: JXArticleFloatWindow.shared.roundEntryView.center)
//        }else {
            return nil
//        }
    }
}
